package com.zhilei;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * aop切面类，对所有方法的实现类进行切面
 *
 * @author 志磊
 */
@Component
@Aspect
@Slf4j
public class ServiceLogAspect {

    // 定义切入点，获截com.zhilei.service.*包下的所有类中所有方法
    @Around("execution(* com.zhilei.service.*.*(..))")
    public Object servicePointcut(ProceedingJoinPoint joinPoint) throws Throwable {

        StopWatch stopWatch = new StopWatch();

        stopWatch.start("execution(com.zhilei.service)AOP 开始执行");

        // 用于执行目标方法
        Object proceed = joinPoint.proceed();
        // 获取方法名
        String targetClassName = joinPoint.getTarget().getClass().getName();
        // 用于获取被调用方法的名称
        String name = joinPoint.getSignature().getName();
        // 拼装
        String joinName = targetClassName + "." + name;

        stopWatch.stop();

        // 打印出任务的耗时统计
        System.out.println(joinName + stopWatch.prettyPrint());
        System.out.println(joinName + stopWatch.shortSummary());
        System.out.println("所有任务总耗时：" + stopWatch.getTotalTimeMillis());
        System.out.println("任务总数：" + stopWatch.getTaskCount());
        return proceed;
    }
}





















